package com.job.admin.audit.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.j2objc.annotations.AutoreleasePool;
import com.job.admin.audit.mapper.AuditPostsMapper;
import com.job.admin.audit.service.AuditPostsService;
import com.job.admin.model.audit.constant.StatusConstant;
import com.job.admin.model.audit.dto.AuditAvatarPageDto;
import com.job.admin.model.audit.dto.AuditPostsPageDto;
import com.job.admin.model.audit.pojo.AuditAvatar;
import com.job.admin.model.audit.pojo.AuditCompany;
import com.job.admin.model.audit.pojo.AuditPosts;
import com.job.client.common.result.AppHttpCode;
import com.job.client.common.result.ResponseResult;
import com.job.client.common.utils.DateTimeUtils;
import com.job.client.model.posts.pojo.Posts;
import com.job.client.model.rabbitmq.AuditResultConstant;
import com.job.feign.client.auth.AuthFeign;
import com.job.feign.client.posts.PostsFeign;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuditPostsServiceImpl extends ServiceImpl<AuditPostsMapper, AuditPosts> implements AuditPostsService {
    @Autowired
    private AuditPostsMapper auditPostsMapper;
    @Autowired
    private AuthFeign authFeign;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private PostsFeign postsFeign;
    @Override
    public ResponseResult getList(Integer page, Integer size, AuditPostsPageDto dto) {
        if(page==null){
            page=1;
        }
        if(size==null){
            size=5;
        }
        QueryWrapper<AuditPosts> queryWrapper=new QueryWrapper<>();
        if(dto==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(!("".equals(dto.getAuditUser()))){
            queryWrapper.eq("audit_user",dto.getAuditUser());
        }
        if(!("".equals(dto.getPostsUser()))){
            queryWrapper.eq("user_id",dto.getPostsUser());
        }
        if(!("".equals(dto.getPostsStatus()) || dto.getPostsStatus()==null)){
            queryWrapper.eq("status",dto.getPostsStatus());
        }else{
            queryWrapper.eq("status", StatusConstant.STATUS_TO_REVIEWED);
        }
        Page<AuditPosts> data=new Page<>(page,size);
        auditPostsMapper.selectPage(data,queryWrapper);
        return ResponseResult.OkResult(data);
    }
    @Override
    public ResponseResult getUserInfo(String userId) {
        ResponseResult result = authFeign.getUserInfoById(userId);
        if(result.getCode()!=200){
            return result;
        }
        return ResponseResult.OkResult(result.getData());
    }
    @Override
    public ResponseResult auditSuccess(String auditPostsId,String userNumber) {
        if(auditPostsId==null || userNumber==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        UpdateWrapper<AuditPosts> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status", StatusConstant.STATUS_AUDIT_SUCCESS)
                .set("audit_user",userNumber)
                .set("audit_time", DateTimeUtils.createNowTime())
                .eq("audit_posts_id",auditPostsId);
        int i = auditPostsMapper.update(updateWrapper);
        if(i!=1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        // 使用rabbitmq修改client中的头像信息
        Map<String,String> data = new HashMap<>();
        data.put("auditPostsId",auditPostsId);
        data.put("result",String.valueOf(StatusConstant.STATUS_AUDIT_SUCCESS)); //审核通过
        rabbitTemplate.convertAndSend(
                AuditResultConstant.EXCHANGE_AUDIT_RESULT,
                AuditResultConstant.ROUTINGKEY_POSTS_RESUT,
                JSON.toJSONString(data));
        return ResponseResult.OkResult();
    }
    @Override
    public ResponseResult auditError(String auditPostsId, String userNumber,String errorMessage) {
        if(auditPostsId==null || userNumber==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        UpdateWrapper<AuditPosts> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status", StatusConstant.STATUS_AUDIT_ERROR)
                .set("audit_user",userNumber)
                .set("reason",errorMessage)
                .set("audit_time", DateTimeUtils.createNowTime())
                .eq("audit_posts_id",auditPostsId);
        int i = auditPostsMapper.update(updateWrapper);
        if(i!=1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        // 使用rabbitmq修改client中的头像信息
        Map<String,String> data = new HashMap<>();
        data.put("auditPostsId",auditPostsId);
        data.put("result", String.valueOf(StatusConstant.STATUS_AUDIT_ERROR)); //审核未通过
        rabbitTemplate.convertAndSend(
                AuditResultConstant.EXCHANGE_AUDIT_RESULT,
                AuditResultConstant.ROUTINGKEY_POSTS_RESUT,
                JSON.toJSONString(data));
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult getPostsInfo(String postsId) {
        if(postsId.charAt(postsId.length()-1) == '='){
            postsId = postsId.substring(0,postsId.length()-1);
        }
        ResponseResult result = postsFeign.getPostsInfo(postsId);
        if(result.getCode() != 200){
            return result;
        }
        Posts posts = JSON.parseObject(JSON.toJSONString(result.getData()), Posts.class);
        if(posts == null){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult(posts);
    }

    @Override
    public ResponseResult getPostsResultById(String auditPostsId) {
        if(auditPostsId==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        AuditPosts auditPosts = auditPostsMapper.selectById(auditPostsId);
        if(auditPosts == null){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult(auditPosts);
    }

    /*
     * 获得该用户未审核的数据
     * */
    public ResponseResult getAuditInfo(String userId){
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        QueryWrapper<AuditPosts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("status",StatusConstant.STATUS_TO_REVIEWED);
        List<AuditPosts> auditPostsList = auditPostsMapper.selectList(queryWrapper);
        return ResponseResult.OkResult(auditPostsList);
    }

}
